\phantomsection
\chapter{Indoor Localization Implementation} % Main chapter title
\label{c5_implementation} % For referencing the chapter elsewhere, use \ref{Chapter1} 
\rhead[\emph{Implementation}]{\thepage}
\lhead[\thepage]{\emph{Implementation}}
\section{Android Application}
On this chapter we implement the Wi-Fi based localization method on an Android smartphone.
\begin{description}
\item[Phone]\hfill\\
 HTC Desire
\item[OS version]\hfill\\
 Android 4.1.2
\end{description}
\begin{figure}
	\centering
		\scalebox{0.2}{\includegraphics{img/app}}
		\rule{35em}{0.5pt}
	\caption{Demonstration App}
	\label{fig:app}
\end{figure}
The Android application (see screenshot\ref{fig:app}) basicly serves two functions: \textit{Logging} and \textit{Classification}.  As we discussed in section \ref{def:fingerprint_method}, the basic concept of fingerprint based localization is based on a fingerprint database and a mapping method from new measurement to the entry in database so that a suitable label (room) can be found, which means room level localization. As we don't have a real database, simply a file, we call the first process \textit{Logging}. On the other hand, since we are using machine learning algorithms as the mapping method, we call the mapping method \textit{Classification}.\\
Figure \ref{fig:app_functions} on page \pageref{fig:app_functions} show the basic functions of the app. \textit{Logging} is implemented in "Scan and Log" and "Generate" process. \textit{Classification} is implemented in a Android service called "Localization Service".
\begin{description}
\item[Start]\hfill\\
Starting of the application, a working directory has to be set, all files will be generated and searched in this directory.
\item[Scan]\hfill\\
Scan the Wi-Fi fingerprint, at the mean time, the room label where the scanning is processed must be provided. On screenshot\ref{fig:app}, room label is provided in the text box and scan is started via the button labelled as "Start Scan". Scan result, the fingerprint is shown in the lower part of the screen.
\item[Log]\hfill\\
Write the Wi-Fi fingerprint, as well as the room label, to a log file.
\item[Generate]\hfill\\
Generate the \textit{arff} file, formatted as \ref{sample_arff} on page \pageref{sample_arff}. The reason why we not directly writing to the arff file is that the \textit{arff} requires all labels, which is not available in each single scan. In the app, this function is started via press button labelled "Create arff.file", see screenshot \ref{fig:app}.
\item[Service]\hfill\\
This is an Android service which provides localization service based on classification result. In the app, starting and stopping service is called by press "Start Weka" and "Stop Weka" respectively, see screenshot \ref{fig:app}. When the service is on, there's a time interval between each classification so that the classification is not so frequent due to signal change, but via the "Wifi Scan" button, we can also invoke a classification. The classification result is shown in the middle part of the screen. The service is supposed to run in background as long as no one needs it any more, but in the demonstration App we run it as long as the App runs or stop it manually. 
\item[Shut down]\hfill\\
Shut down the app, in the demonstration app, the service is also shut down. But in the really usage, the service will be keeping functional until no other app needs it any more.
\end{description}
Figure \ref{fig:app_service} on page \pageref{fig:app_service} is the detail demonstration on how the localization service works. The figure \ref{fig:app_service} shows two tasks for the service,generating arff file (path 1) and classifying (path 2).
\begin{description}
\item[Generate \textit{Arff}]\hfill\\
The whole classification starts with a fingerprint database, here an \textit{arff} file. We have to make sure the presence of this file. Although the file can be generated manually  with the basic functions of the app, we generated it again in the service in case it's not present. This function only called once when the service starts.
\item[Classify]\hfill\\
This is the part where machine learning algorithm works. This task is a loop, starting with scanning Wi-Fi fingerprints. The scanning can be invoked manually or any change to the current Wi-Fi environment, such as access point signal strength changed. Once we get a fingerprint, we use the classifier, defined by selected machine learning algorithm, to classify the measurement and broadcast the result (room label and the confidence in percentage of this result). Then we wait for the next fingerprint and the loop goes on. If the classifier is not trained yet, we can training it either on model or the existing \textit{arff} file. The model is a file of serialized \textit{WEKA} classifier, it has been train by any specific machine learning algorithm.  If the model file is not present we can train our classifier with pre-selected machine learning algorithms and  \textit{arff} file. Once the classifier is trained(initialized) or there is no \textit{arff} file yet, we end the current loop and wait for the next fingerprint.
\end{description}
The demonstration App give a clear way of implementing machine learning based Wi-Fi fingerprint technology. To be general, we only need two things, a database filled with Wi-Fi fingerprints and a algorithm for classifier training, in our case, random forest with 20 trees. Then the localization result is provided.
\begin{figure}
\centering
\tikzstyle{decision} = [diamond, draw, fill=blue!20,
    text width=4.5em, text badly centered, node distance=2.5cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
    text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{file} = [rectangle, draw, fill=red!20,
    text width=5em, text centered, minimum height=4em]
\tikzstyle{line} = [draw, very thick, color=black!50, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=2.5cm,
    minimum height=2em]
\tikzstyle{decision out}=[near start,color=black]
\begin{tikzpicture}[scale=1, node distance = 2.5cm, auto]
    % Place nodes
 	\node [block] (start) {Start App};
    \node [block,below of =start, left of=start] (collect) {Scan and Log};
    \node [block,below of =start, right of=start] (startService) {Start Service};
    \node [block,below of =start] (generate) {Generate};
    \node[file,below of =collect](log){Log File};
    \node[file,below of =generate](arff){Arff File};
    \node[block,below of =startService](localization){Localization};
    \node[block,below of =arff](end){Shut Down};
    % Draw edges
  	\path[line](start)-|(collect);
  	\path[line](start)--(generate);
  	\path[line](start)-|(startService);
  	\path[line](collect)--(log);
    \path[line](generate)--(arff);
  	\path[line](startService)--(localization);
   	\path[line](log)|-(end);
  	\path[line](arff)--(end);
  	\path[line](localization)|-(end);
\end{tikzpicture}
\caption{\label{fig:app_functions} Basic Functions of the App }
\end{figure}


\begin{figure}
\centering
\tikzstyle{decision} = [diamond, draw, fill=blue!20,
    text width=4.5em, text badly centered, node distance=2.5cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
    text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{file} = [rectangle, draw, fill=red!20,
    text width=5em, text centered, minimum height=4em]
\tikzstyle{line} = [draw, very thick, color=black!50, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=2.5cm,
    minimum height=2em]
\tikzstyle{decisionOut}=[near start,color=black]

\begin{tikzpicture}[scale=1, node distance = 3cm, auto]
    % Place nodes
	\node [block] (start) {Start Service};
	
    \node [decision,below of =start, left of=start] (isLog) {Log File Exist?};
    \node [decision,below of =isLog, node distance = 3.5cm] (isLogChanged) {Log File Changed?};
    \node [decision,below of =isLogChanged, node distance = 3.5cm] (isArff) {Arff File Exist?};
	\node[block,left of =isArff](log2arff){Convet Log File to Arff File};
	
	\node [block,below of =start, right of=start,node distance = 2.5cm] (scan) {Scan WiFi Signal};
	\node [decision,below of =scan, node distance = 3cm] (isIni) {Classifier Initialized?};
	\node [block, right of=isIni,node distance = 3.5cm] (classify) {Classify};
	\node [block, above of=classify] (broadcast) {Location Broadcast};
	\node [decision,below of =isIni, node distance = 3.5cm] (isModel) {Model Exists?};
	\node [block, below of=isModel] (iniModel) {Initialize Classifier with Model};
	\node [block, left of=iniMode,below of =iniModel] (wait) {Wait for Next Scan};
	\node [decision, right of=isModel,, node distance = 3cm] (isArffForModel) {Arff File Exists?};
	\node [block, ,below of =iniModel,right of=iniModel] (end) {Current Loop End};
	\node [block, right of=isArffForModel,below of =isArffForModel] (iniArff) {Initialize Classifier with Arff File};
    % Draw edges
    \path[line](start)-|node[decisionOut] {1}(isLog);
    \path[line](start)-|node[decisionOut] {2}(scan);
    
	\path[line] (isLog) -- node[decisionOut] {yes} (isLogChanged);
	\path[line] (isLogChanged) -- node[decisionOut] {yes} (isArff);
	\path[line] (isArff) -- node[decisionOut] {yes} (wait);
	\path[line] (isArff) -- node[decisionOut] {no} (log2arff);
	
	\path[line] (scan) --  (isIni);
	\path[line] (isIni) -- node[decisionOut] {yes} (classify);
	\path[line] (classify) --  (broadcast);
	\path[line,dashed] (broadcast) --  (scan);
	\path[line] (isIni) -- node[decisionOut] {no} (isModel);
	\path[line] (isModel) -- node[decisionOut] {yes} (iniModel);
	\path[line] (isModel) -- node[decisionOut] {no} (isArffForModel);
	\path[line] (isArffForModel) -- node[decisionOut] {yes} (iniArff);
	\path[line] (isArffForModel) -- node[decisionOut] {no} (end);
	\path[line] (iniModel) --  (end);
	\path[line] (iniArff) --  (end);
	\path[line] (end) --  (wait);
	\path[line,dashed] (wait) |-  (scan);
\end{tikzpicture}
\caption{\label{fig:app_service} Basic Functions of the App }
\end{figure}